import moment from 'moment'

// BusEvent名称
export const EVENT_NAME = {
  projectCreate: 'project-create',
  taskList: 'task-list'
}

// 表格搜索
export function searchEmit () {
  this.$refs.tableSearch.assignParams(this.params)
  this.$refs.table.refresh(true)
}

// 表格重置
export function resetEmit () {
  this.$refs.tableSearch.resetParams(this.params)
  this.$refs.table.refresh(true)
}

// 页面滚动到指定位置
export function scrollToTop (num) {
  const ScrollTop = (number = 0, time) => {
    if (!time) {
      document.body.scrollTop = document.documentElement.scrollTop = number
      return number
    }
    const spacingTime = 20 // 设置循环的间隔时间  值越小消耗性能越高
    let spacingInex = time / spacingTime // 计算循环的次数
    let nowTop =
      document.body.scrollTop + document.documentElement.scrollTop // 获取当前滚动条位置
      const everTop = (number - nowTop) / spacingInex // 计算每次滑动的距离
    const scrollTimer = setInterval(() => {
      if (spacingInex > 0) {
        spacingInex--
        ScrollTop((nowTop += everTop))
      } else {
        clearInterval(scrollTimer) // 清除计时器
      }
    }, spacingTime)
  }
  ScrollTop(num, 300)
}

// 获取元素的offset
export function getDomOffset (dom, distance) {
  distance = distance || 0
  if (['BODY', null].includes(dom.offsetParent.nodeName)) {
    distance += dom.offsetTop
    return distance
  } else {
    distance += dom.offsetTop
    return getDomOffset(dom.offsetParent, distance)
  }
}

// 赋值滚动到必填元素附近
export function handleRequiredScroll () {
  setTimeout(() => {
    const explainMap = document.querySelectorAll('.ant-form-explain')
    if (explainMap.length > 0 && document.documentElement.scrollTop > 0) {
      scrollToTop(getDomOffset(explainMap[0]) - 124)
    }
  })
}

// a-select组件的模糊查询
export function selectFilter (inputValue, option) {
  return (option.componentOptions.children[0].text.toLowerCase().indexOf(inputValue.toLowerCase()) >= 0)
}

// 禁止选择过去的日期
export function disabledDate (current) {
  return current && current < moment().add(-1, 'd')
}

// 清空弹窗表单
export function cleanModalForm (form) {
  for (const key in form) {
    if (Array.isArray(form[key])) {
      form[key] = []
    } else if (typeof form[key] === 'boolean') {
      form[key] = false
    } else {
      form[key] = ''
    }
  }
}

// 前端分页
export function dataToPageGroup (page = 1, pageSize = 10, totalData = []) {
  const { length } = totalData
  const tableData = {
    data: [],
    page,
    pageSize,
    length
  }
  if (pageSize >= length) {
    tableData.data = totalData
    tableData.page = 1
  } else {
    const num = pageSize * (page - 1)
    if (num < length) {
      const startIndex = num
      const endIndex = num + pageSize - 1
      tableData.data = totalData.filter((_, index) => index >= startIndex && index <= endIndex)
    } else {
      const size = parseInt(length / pageSize)
      const rest = length % pageSize
      if (rest > 0) {
        tableData.page = size + 1
        tableData.data = totalData.filter((_, index) => index >= (pageSize * size) && index <= length)
      } else if (rest === 0) {
        tableData.page = size
        tableData.data = totalData.filter((_, index) => index >= (pageSize * (size - 1)) && index <= length)
      }
    }
  }
  return tableData
}

// 生成唯一id
export function uuid () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    const r = Math.random() * 16 | 0
    const v = c === 'x' ? r : (r & 0x3 | 0x8)
    return v.toString(16)
  })
}
